#include "volumetool.h"
#include "ui_volumetool.h"
#include <QDebug>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),isMuted(false)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    QGraphicsDropShadowEffect* shadowEffect=new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0,0);
    shadowEffect->setColor("#000000"); //黑色
    shadowEffect->setBlurRadius(15);
    this->setGraphicsEffect(shadowEffect);

    //设置初始音量为20%
    ui->volumeRate->setText("20%");

    //设置外滑杆的位置
    QRect rect=ui->outline->geometry();
    int outlineLenth=ui->outline->height()*0.2;
    ui->outline->setGeometry(rect.x(),rect.y()+ui->outline->height()-outlineLenth,rect.width(),outlineLenth);

    //设置滑杆按钮的位置
    ui->sliderBt->move(ui->sliderBt->x(),ui->outline->y()-ui->sliderBt->height()/2);

    //安装事件过滤器
    ui->volumeBox->installEventFilter(this);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::on_silenceBt_clicked()
{
    isMuted=!isMuted;

    if(isMuted==true)
    {
        ui->silenceBt->setStyleSheet("#silenceBt{background-image:url(\":/res/muted.png\");background-repeat:no-repeat;background-position:center center;}");
    }
    else
    {
        ui->silenceBt->setStyleSheet("#silenceBt{background-image:url(\":/res/volume.png\");background-repeat:no-repeat;background-position:center center;}");
    }

    emit mutedClick(this->isMuted);
   // qDebug()<<"emited mutedClick(this->isMuted);";
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
  (void)event;
   QPainter painter(this);
   //设置画笔
   painter.setPen(Qt::NoPen);
   //设置笔刷
   painter.setBrush(QBrush("#5688E1"));

   //绘制三角形
   QPolygon polygon;
   QPoint a(25,300);
   QPoint b(75,300);
   QPoint c(50,300+30);
   polygon.append(a);
   polygon.append(b);
   polygon.append(c);

   painter.drawPolygon(polygon);
}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==ui->volumeBox)
    {
        if(event->type()==QEvent::MouseButtonPress)    //鼠标点击
        {
            calVolumeVal();

        }
        else if(event->type()==QEvent::MouseMove)   //鼠标移动
        {
            calVolumeVal();
            emit this->setVolumeVal(this->volumeVal);
        }
        else if(event->type()==QEvent::MouseButtonRelease)  //鼠标释放
        {
            emit this->setVolumeVal(this->volumeVal);
        }

        return true;
    }

    return QObject::eventFilter(watched, event);
}

void VolumeTool::calVolumeVal()
{
    //获取鼠标点击时的y坐标
    int height=ui->volumeBox->mapFromGlobal(QCursor().pos()).y();

    //验证y坐标的合法性
    height=height<25?25:height;
    height=height>205?205:height;

    //更新outline大小
    ui->outline->setGeometry(ui->outline->x(),height,ui->outline->width(),205-height);

    //更新滑杆的位置
    ui->sliderBt->move(ui->sliderBt->x(),ui->outline->y()-ui->sliderBt->height()/2);

    //计算音量大小
    this->volumeVal=(int)((ui->outline->height()/(float)180)*100);

    //更新音量百分比
    ui->volumeRate->setText(QString::number(this->volumeVal)+"%");
}




























